//TableEditSubmit_INC.js

function  Formsubmit(Form, ReturnAction) {


	if(ReturnAction){
		Form.action = Form.action + "/ReturnAction/" +  ReturnAction;
	}

	var ProductSubTypePass = false;
	if($("#ThirdProductSubTypeID").is(":visible")){

		if($("#ThirdProductSubTypeID option:selected").attr("value") != "0"){
			ProductSubTypePass = true;
		}
	}


	var InputPass = true;
	var InputTitle = "";
	var inputObject = $("input[required='true']");

	inputObject.each(function(){ 
			if(this.value == '') { 
				$(this).focus().select();
				InputPass = false; 
				InputTitle = $(this).attr("title");
				return false;
			}
		}
	);
	
	var ColorPass = false;
	var inputObject = $("input[field='ProductColor']");
	
	inputObject.each(function(){ 
			if(this.value != '') { 
				ColorPass = true; 
				return true;
			}
		}
	);

	var SizePass = false;
	var inputObject = $("input[field='ProductSize']");
	
	inputObject.each(function(){ 
			if(this.value != '') { 
				SizePass = true; 
				return true;
			}
		}
	);
	var SelectCountryPass = false;
	if($("#country_of_origin option:selected").attr("value") != ""){
		SelectCountryPass = true;
	}

	var SelectSoleMaterialPass = false;
	if( $("#sole_material option:selected").attr("value") != "" ){
		SelectSoleMaterialPass = true;
	}

	if(ProductSubTypePass && InputPass && ColorPass && SizePass && SelectCountryPass && SelectSoleMaterialPass ){
		setTimeout(function () {
			Form.submit();
			return true;
		}, 100);
	}
	else{
		if(!ProductSubTypePass){
			alert("商品类型最少需要选择三级节点，请准确选择.");
		}
		else if(!InputPass){
			alert(InputTitle + "不能为空，请准确填写.");
		}
		else if(!SelectCountryPass){
			alert("生产国需要选择。");
		}
		else if(!SelectSoleMaterialPass){
			alert("鞋底材质需要选择。");
		}
		else if(!ColorPass){
			alert("产品颜色 至少填写一个。");
		}
		else if(!SizePass){
			alert("产品尺码 至少填写一个。");
		}
		return false;
	}
}



jQuery(document).ready(function () { 
	var ProductSubType = $("#ProductSubTypeID"); 
	var SecondProductSubType = $("#SecondProductSubTypeID"); 
	var ThirdProductSubType = $("#ThirdProductSubTypeID"); 
	var FourthProductSubType = $("#FourthProductSubTypeID"); 
	var FifthProductSubType = $("#FifthProductSubTypeID"); 


//填充数据 
//绑定事件，触发事件后填充下一个级别的数据 
	jQuery(ProductSubType).bind("change keyup", function () { 
		var ProductSubTypeID = ProductSubType.val(); 
		loadAreas(ProductSubTypeID, "SecondProductSubTypeID"); 
		//SecondProductSubType.fadeIn("slow"); 
	}); 

//给下一个绑定事件，触发事件后填充下一个级别数据 
	jQuery(SecondProductSubType).bind("change keyup", function () { 
		var SecondProductSubTypeID = SecondProductSubType.val(); 
		loadAreas(SecondProductSubTypeID, "ThirdProductSubTypeID"); 
	}); 

	//给下一个绑定事件，触发事件后填充下一个级别数据 
	jQuery(ThirdProductSubType).bind("change keyup", function () { 
		var ThirdProductSubTypeID = ThirdProductSubType.val(); 
		loadAreas(ThirdProductSubTypeID, "FourthProductSubTypeID"); 
	}); 

//给下一个绑定事件，触发事件后填充下一个级别数据 
	jQuery(FourthProductSubType).bind("change keyup", function () { 
		var FourthProductSubTypeID = FourthProductSubType.val(); 
		loadAreas(FourthProductSubTypeID, "FifthProductSubTypeID"); 
	});


	
}); 


function loadAreas(val, item) { 
	
	
	var SecondProductSubType = $("#SecondProductSubTypeID"); 
	var ThirdProductSubType = $("#ThirdProductSubTypeID"); 
	var FourthProductSubType = $("#FourthProductSubTypeID"); 
	var FifthProductSubType = $("#FifthProductSubTypeID"); 

	switch(item)
	{
		case "SecondProductSubTypeID":
			if(val == "0"){
				SecondProductSubType.empty().hide();
			}
			ThirdProductSubType.empty().hide();
			FourthProductSubType.empty().hide();
			FifthProductSubType.empty().hide();
		  break;
		case "ThirdProductSubTypeID":
			if(val == "0"){
				ThirdProductSubType.empty().hide();
			}
			FourthProductSubType.empty().hide();
			FifthProductSubType.empty().hide();
		  break;
		case "FourthProductSubTypeID":
			if(val == "0"){
				FourthProductSubType.empty().hide();
			}
			FifthProductSubType.empty().hide();
			break;
	}

	jQuery.ajax({ 
		type: "POST", 
		url: APP + "/Ajax/AjaxProductSubType", 
		data: {Code: val, Class:item},
		dataType: "json",
		cache:false,
		error: function () { 
			return false;
		}, 
		success: function (data) { 
			var json = data;
			var i; 
			$("#" + item).empty();
			if(json.length > 0){
				jQuery("#" + item).append("<option value='0' selected='selected'>请选择</option>"); 
				for (i = 0; i < json.length; i++) { 
					jQuery("#" + item).append(jQuery("<option></option>").val(json[i].Code).html(json[i].ProductSubType)); 
				}
				$("#" + item).show();
			}
			else{
			$("#" + item).hide();
			}
			
			
		} 
	}); 

} 

function ShowNextProductSize(ObjectID){

	$("#" + ObjectID + "").fadeIn('slow');
}

function ShowNextProductColor(ObjectID){

	$("#" + ObjectID + "").fadeIn('slow');
}

function CreatMoreProductSizeTr(ObjectID){
	var Object = $("#"+ ObjectID);
	if(Object.attr("ProductSizeCurrentRow") < 5){
		var CloneHtml = $("#CloneProductSizeRow");
		$("#ProductSizeRow" + Object.attr("ProductSizeCurrentRow")).after("<tr id=ProductSizeRow" + (parseInt(Object.attr("ProductSizeCurrentRow")) + 1)+ ">" + CloneHtml.html().trim() + "</tr>");
		Object.attr("ProductSizeCurrentRow", parseInt(Object.attr("ProductSizeCurrentRow")) + 1);
	}
}

function CreatMoreProductColorTr(ObjectID){
	var Object = $("#"+ ObjectID);
	if(Object.attr("ProductColorCurrentRow") < 5){
		var CloneHtml = $("#CloneProductColorRow");
		$("#ProductColorRow" + Object.attr("ProductColorCurrentRow")).after("<tr id=ProductColorRow" + (parseInt(Object.attr("ProductColorCurrentRow")) + 1)+ ">" + CloneHtml.html().trim() + "</tr>");
		Object.attr("ProductColorCurrentRow", parseInt(Object.attr("ProductColorCurrentRow")) + 1);
	}
}

function DeleteProductSize(ProductSize){
	var ImportProductSheetID = $("#ImportProductSheetID").val();
	
	jQuery.ajax({ 
		type: "POST", 
		url: APP + "/Ajax/DeleteProductSize", 
		data: {ImportSheetTableID: ImportProductSheetID, ProductSize:ProductSize, TemplateType:"Shoes"},
		dataType: "text",
		cache:false,
		error: function () { 
			return false;
		}, 
		success: function (data) {
			var newData = data.replace("\n","").replace(/(^\s*)|(\s*$)/g, "");
			if(newData == "ok"){
				$("input[ProductSize="+ProductSize+"]").remove();
				$("#" + ProductSize + "Icon").remove();
				if($("#ProductSizeRow1 input").length == 0){
					$("input[field=ProductColor]").each(function(){ 
						if(this.value != ""){
							$("#" + $(this).val() + "Icon").remove();
							$(this).remove();
						}
					});
				}
				alert("删除成功");
			}
			else{
				alert("删除失败");
			}
		} 
	}); 
}


function DeleteProductColor(ProductColor){
	var ImportProductSheetID = $("#ImportProductSheetID").val();
	
	jQuery.ajax({ 
		type: "POST", 
		url: APP + "/Ajax/DeleteProductColor", 
		data: {ImportSheetTableID: ImportProductSheetID, ProductColor:ProductColor, TemplateType:"Shoes"},
		dataType: "text",
		cache:false,
		error: function () { 
			return false;
		}, 
		success: function (data) {
			var newData = data.replace("\n","").replace(/(^\s*)|(\s*$)/g, "");
			if(newData == "ok"){
				$("input[ProductColor="+ProductColor+"]").remove();
				$("#" + ProductColor + "Icon").remove();
				if($("#ProductColorRow1 input").length == 0){
					$("input[field=ProductSize]").each(function(){ 
						if(this.value != ""){
							$("#" + $(this).val() + "Icon").remove();
							$(this).remove();
						}
					});
				}
				alert("删除成功");
			}
			else{
				alert("删除失败");
			}
		} 
	}); 
}